﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Caching;

namespace MyAnim.Controllers
{
    public class FilesController : Controller
    {
        //
        // GET: /Files/

        public ActionResult Index()
        {
            
            return View();
        }

        public ActionResult GetFile()
        {
            Models.FileModel model = null;
            String ShortName = String.Empty;
            ShortName = (Request.Params["d"] == null) ? String.Empty :Request.Params["d"];
            try
            {
                if (ShortName.Length > 10)
                {
                    return View("NotFound");
                }

                
                var files = (from r in Models.AnimContext.GetContext.FileInfo
                             where r.ShortName == ShortName
                             select r);
                
                ////Check if a file was found
                //if (files.Count() == 0)
                //{
                //    return View("NotFound");
                //}

                HttpRuntime.Cache.Add(ShortName, "http://www.free.fr", null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 2, 0), CacheItemPriority.Default, null);

               //HttpRuntime.Cache.Add(ShortName, files.ToArray()[0].GetHttpLocation(), null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 2, 0), CacheItemPriority.Default, null);
               model = new Models.FileModel(ShortName, "");

                return View(model);
              // return View(new Models.FileModel(file.GetHttpLocation()));
              //  return View(new Models.FileModel(files.ToArray()[0].GetHttpLocation()));
            }
            catch (Exception)
            {
                return View("NotFound");
            }
           
        }

        [HttpPost]
        public String GetHttpLocation(String Name)
        {
            return (String)HttpRuntime.Cache[Name];
        }


        //
        // GET: /Files/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Files/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Files/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Files/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Files/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Files/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Files/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult NotFound()
        {
            return View();
        }
    }
}
